import 'package:flutter/material.dart';
import 'package:xv_flutter/model/news.dart';
import 'package:xv_flutter/router/application.dart';
import 'package:xv_flutter/utils/http.dart';

class NewsPage extends StatefulWidget {
  @override
  _NewsPageState createState() => _NewsPageState();
}

class _NewsPageState extends State<NewsPage> {
  List<Items> list;

  @override
  void initState() {
    _getNews();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (list != null) {
      return Scaffold(
          body: ListView.separated(
        scrollDirection: Axis.vertical,
        itemBuilder: (BuildContext context, int index) {
          Items item = list[index];
          return ListTile(
            title: Text(item.title),
            subtitle: Text(item.content),
            leading: Icon(Icons.fiber_new),
            trailing: Icon(Icons.arrow_forward),
            contentPadding: EdgeInsets.all(10),
            enabled: true,
            onTap: () {
              Application.router.navigateTo(context, "/news_detail?id=12");
            },
          );
        },
        separatorBuilder: (BuildContext context, int index) => Divider(
          height: 1,
          color: Colors.grey,
        ),
        itemCount: list.length,
      ));
    } else {
      return Text('');
    }
  }

  // 获取产品列表
  _getNews() async {
    var fromData = {
      "page": 10,
    };
    await request('newsPage', formData: fromData).then((resp) {
      NewsModel newsPage = NewsModel.fromJson(resp);
      setState(() {
        list = newsPage.data.items;
      });
      // print(homePage.toJson());
    }).catchError((e) {
      print("Error ===> $e");
    });
  }
}
